package com.mpyy.handler.purchase;

import com.alibaba.fastjson.JSONObject;
import com.mpyy.pojo.InventoryChange;
import com.mpyy.pojo.InventoryDesc;
import com.mpyy.pojo.PurchaseStock;
import com.mpyy.pojo.PurchaseStockDesc;
import com.mpyy.service.purchase.SearchProdutAndproTypeServiceInter;
import com.mpyy.service.purchase.UpdatePurchaseServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @version 1.0
 * @Descoription updatePurchaseDescHandler
 * @Author CG
 * @date 2022-03-21
 */
@Controller
public class UpdatePurchaseDescHandler {
        @Autowired
        @Qualifier(value = "searchProdutAndproTypeServiceImpl")
        private SearchProdutAndproTypeServiceInter serviceInter;
        @Autowired
        @Qualifier(value = "updatePurchaseServiceImpl")
        private UpdatePurchaseServiceInter purchaseServiceInter;
        @RequestMapping(value = "/updatePurchaseDesc/{purchaseStock}/{purchaseStockDesc}")
        @ResponseBody
        public  String updatePurchaseDesc(@PathVariable String purchaseStock,@PathVariable String purchaseStockDesc)throws Exception{
            //实例化一个采购订单表
            PurchaseStock purStock =new PurchaseStock();
            JSONObject o = JSONObject.parseObject(purchaseStock);
            //采购进货单号
            purStock.setPurStoId(o.getString("purStoId"));
            //供货商编号
            purStock.setSupId(o.getString("supId"));
            //收货仓库编号
            purStock.setDepotId(o.getString("depotId"));
            //进货日期
            purStock.setPurStoDate(o.getDate("purStoDate"));
            //员工编号（经办人）
            purStock.setEmpId(o.getString("empId"));
            //应付金额
            purStock.setPurStoPayable(o.getDouble("purStoPayable"));
            //实付金额
            purStock.setPurStoOutof(o.getDouble("purStoOutof"));
            //原始单号
            purStock.setPurStoOriginalNum(o.getString("purStoOriginalNum"));
            //备注
            purStock.setPurStoCommment(o.getString("purStoCommment"));
            //计划日期
            purStock.setPurStoPreDate(o.getDate("purStoPreDate"));
            System.out.println(purStock);

        //实例化一个采购订单明细对象
            PurchaseStockDesc stockDesc = new PurchaseStockDesc();
            //创建一个StringBuffer对象，获取页面传递过来的销售订单明细数据，拼接还原后加入StringBuffer中
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append("[" + purchaseStockDesc + "]");
            //采购详情表信息
            List<PurchaseStockDesc> purchaseStockDescs= ProSalesUtil.<PurchaseStockDesc>decomposeJsonArray(stringBuffer, PurchaseStockDesc.class);

            //保存仓库详情表信息
            List<InventoryDesc> inventoryDescList =new ArrayList<>();
            //保存库存变动信息
            List<InventoryChange> changeList =new ArrayList<>();
            int i =0;
            for (PurchaseStockDesc desc : purchaseStockDescs) {
                InventoryDesc inventoryDesc = new InventoryDesc();
                InventoryChange inventoryChange= new InventoryChange();
                i++;
                Calendar calendar =Calendar.getInstance();

                String   year   =   String.valueOf(calendar.get(Calendar.YEAR));
                String   month   =   String.valueOf(calendar.get(Calendar.MONTH)+1);
                if(month.equals("10")){
                    month="0"+month;
                }

                String   day   =   String.valueOf(calendar.get(Calendar.DATE));
                String   hour   =   String.valueOf(calendar.get(Calendar.HOUR));
                String   minute   =   String.valueOf(calendar.get(Calendar.MINUTE));
                String   second   =   String.valueOf(calendar.get(Calendar.SECOND)+i);
                //库存商品明细编号
                inventoryDesc.setInveDescId("KC"+year+month+day+hour+minute+second);
                //商品编号
                inventoryDesc.setProId(desc.getProId());
                //商品名称
                inventoryDesc.setInveDescName(desc.getProName());
                //商品类别
                inventoryDesc.setInveDescType(serviceInter.searchProdutAndtype(desc.getProId()));
                //单位
                inventoryDesc.setInveDescUnit(desc.getPurStoDescUnit());
                //产品规格
                inventoryDesc.setInveDescSize(desc.getPurchaseSize());
                //批准文号
                inventoryDesc.setInveDescPostil(desc.getPurPostil());
                //生产厂商
                inventoryDesc.setInveDescManufacturer(desc.getPurStoDescManufacturer());
                //批号
                inventoryDesc.setInveDescBatchNum(desc.getProBatchNum());
                //供货商编号
                inventoryDesc.setSupId(purStock.getSupId());
                //生产日期
                inventoryDesc.setInveDescStartDate(desc.getPurStoDescStartDate());
                //有效期
                inventoryDesc.setInveDescValidityDate(desc.getPurchaseProValidityDate());
                //库存数量
                inventoryDesc.setInveDescInventoryNum(desc.getPurStoDescNum());
                //单价（成本价）
                inventoryDesc.setInveDescPrice(desc.getPurStoDescPrice());
                //所在仓库
                inventoryDesc.setInveDescDepot(purStock.getDepotId());
                //商品状态
                inventoryDesc.setInveDescProState(1);
                inventoryDescList.add(inventoryDesc);


                //库存变动编号
                inventoryChange.setInvTurnId(0);
                //商品编号
                inventoryChange.setProId(desc.getProId());
               // 商品名称
                inventoryChange.setInvTurnProName(desc.getProName());
                //单位
                inventoryChange.setInvUnitName(desc.getPurStoDescUnit());
                //生产厂商
                inventoryChange.setInvTurnManufacturer(desc.getPurStoDescManufacturer());
                // 规格型号
                inventoryChange.setInvTurnModel(desc.getPurchaseSize());
                //批号
                inventoryChange.setInvTurnBatchNum(desc.getProBatchNum());
                //采购数量
                inventoryChange.setInvPurchaseNum(desc.getPurStoDescNum());
                //商品变动日期
                inventoryChange.setInvTurnDate(o.getDate("purStoDate"));

                changeList.add(inventoryChange);
            }
            for (InventoryDesc inventoryDesc : inventoryDescList) {
                System.out.println(inventoryDesc);
            }
            for (InventoryChange inventoryChange : changeList) {
                System.out.println(inventoryChange);
            }
            for (PurchaseStockDesc desc : purchaseStockDescs) {
                System.out.println(desc);
            }


            int row = purchaseServiceInter.updatePurchaseInfo(purStock, purchaseStockDescs, inventoryDescList, changeList);
            return String.valueOf(row);
        }
}
